#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年3月9日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

import logging
sys.path.append(os.path.join(current_dir,'../../'))
from base.mofi_datas import MofiDatas


import sys,datetime


class APInfos(MofiDatas):
    '''
        ap信息:device , company , shop , status , first online time ,last online time , last offline time
    '''
    def __init__(self):
        pass

    def merger_dict(self):
        pass

    def get_ap_infos(self,devices=[],tab='devicelog',fields=None):
        apInfos = []
        devStatus = self.get_ap_status_from_devices(devices=devices)
        devOnlines = self.get_ap_online_time(devices=devices)
        devOfflines = self.get_ap_offline_time(devices=devices)
        devInfos = self.get_devices_comp_info(devices)
        if fields == None or 'white_list' in fields or 'black_list' in fields:
            devWhiteBlackList = self.get_ap_config( devices=devices)
        
        for device in devices:
            
            apInfo = {}
            
            if device  not in devOnlines.keys():
                devOnlines[device] = {'first_online':'-','last_online':'-'}
                
                
            for dokey , dovalue in devOnlines[device].iteritems():
                if fields==None or dokey in fields:
                    apInfo = devOnlines[device]
                
            if fields==None or 'last_offline' in fields:
                if device in devOfflines.keys():
                    last_offline = devOfflines[device]['last_offline']
                    last_online = apInfo['last_online']
                    if last_online!='-' and last_online > last_offline:
                        last_offline = '-'
                    apInfo['last_offline'] = last_offline
                else:
                    apInfo['last_offline'] ='-'
            if fields == None or 'status' in fields:
                if device in devStatus.keys():
                    apInfo['status'] = devStatus[device]['status']
                else:
                    apInfo['status'] = '-'
            
            if fields == None or 'white_list' in fields or 'black_list' in fields:
                if device in devWhiteBlackList.keys():
                    apInfo['white_list'] = ','.join(devWhiteBlackList[device]['white_list'])
                    apInfo['black_list'] = ','.join(devWhiteBlackList[device]['black_list'])
                else:
                    apInfo['white_list'] = '-'
                    apInfo['black_list'] = '-'
            if device in devInfos.keys():
                for dk,dvalue in devInfos[device].iteritems():
                    if fields == None or dk in fields:
                        apInfo[dk] = dvalue
#                 apInfo['comp_name'] = devInfos[device]['comp_name']
#                 apInfo['group_name'] = devInfos[device]['group_name']
#                 apInfo['group_serial_number'] = devInfos[device]['serial_number']
            apInfo['device'] = device
            apInfos.append(apInfo)
                
        return apInfos
    

if __name__=='__main__':
    myAPInfos = APInfos()
    print myAPInfos.get_ap_infos(devices=['060810741254'])

